--
-- Description: script to create the WER Services database and
-- set it's attributes if it does not exist.
-- Date created: 17 Apr 2008
--

use [master]
go

declare @newDB bit
set @newDB = 0 -- false

if exists (select top 1 1 from sys.databases where name='$(DatabaseName)')
begin
    --
    -- don't create the database since it already exists.
    --  
    print ''
    print '*************************************************************************************'  
    print 'Database $(DatabaseName) already exists.'
    print '*************************************************************************************'  
    print ''
end
else
begin
    --
    -- create the database.
    --
    print ''
    print '*************************************************************************************'  
    print 'Creating database $(DatabaseName).' 
    print '*************************************************************************************'  
    print ''

    create database [$(DatabaseName)] on  primary
    (
        name = N'$(DatabaseName)'
        , filename = N'$(DataFilePath)\$(DatabaseName).mdf' 
        , size = 256000kb
        , maxsize = unlimited
        , filegrowth = 102400kb
    )
    log on
    (
        name = N'$(DatabaseName)_log'
        , filename = N'$(LogFilePath)\$(DatabaseName)_log.ldf'
        , size = 102400kb
        , maxsize = 2048gb
        , filegrowth = 10%
    )

    --
    -- 
    --
     
    set @newDB = 1 -- true
	--
	-- database created message.
	--
    print ''
    print '*************************************************************************************'  
	print 'Database $(DatabaseName) created successfully.' 
    print '*************************************************************************************'  
    print ''
end


if (@newDB = 1)
begin
	--
	-- setting the database attributes
	--
    print ''
    print '*************************************************************************************'  
	print 'Setting database $(DatabaseName) attributes.' 
    print '*************************************************************************************'  
    print ''
    exec dbo.sp_dbcmptlevel @dbname=N'$(DatabaseName)', @new_cmptlevel=90

    if (1 = fulltextserviceproperty('IsFullTextInstalled'))
    begin
        exec [$(DatabaseName)].[dbo].[sp_fulltext_database] @action = 'disable'
    end

    alter database [$(DatabaseName)] set ANSI_NULL_DEFAULT off

    alter database [$(DatabaseName)] set ANSI_NULLS off
    
    alter database [$(DatabaseName)] set ANSI_NULL_DEFAULT off
    
    alter database [$(DatabaseName)] set ANSI_NULLS off
    
    alter database [$(DatabaseName)] set ANSI_PADDING off

    alter database [$(DatabaseName)] set ANSI_WARNINGS off
    
    alter database [$(DatabaseName)] set ARITHABORT off
    
    alter database [$(DatabaseName)] set AUTO_CLOSE off
    
    alter database [$(DatabaseName)] set AUTO_CREATE_STATISTICS on
    
    alter database [$(DatabaseName)] set AUTO_SHRINK off

    alter database [$(DatabaseName)] set AUTO_UPDATE_STATISTICS on
    
    alter database [$(DatabaseName)] set CURSOR_CLOSE_ON_COMMIT off

    alter database [$(DatabaseName)] set CURSOR_DEFAULT  LOCAL
    
    alter database [$(DatabaseName)] set CONCAT_NULL_YIELDS_NULL on

    alter database [$(DatabaseName)] set NUMERIC_ROUNDABORT off
    
    alter database [$(DatabaseName)] set QUOTED_IDENTIFIER on
    
    alter database [$(DatabaseName)] set RECURSIVE_TRIGGERS off
    
    alter database [$(DatabaseName)] set AUTO_UPDATE_STATISTICS_ASYNC off
    
    alter database [$(DatabaseName)] set DATE_CORRELATION_OPTIMIZATION off
    
    alter database [$(DatabaseName)] set PARAMETERIZATION SIMPLE
    
    alter database [$(DatabaseName)] set  READ_WRITE
    
    alter database [$(DatabaseName)] set RECOVERY SIMPLE
    
    alter database [$(DatabaseName)] set  MULTI_USER
    
    alter database [$(DatabaseName)] set PAGE_VERIFY CHECKSUM
    
    print ''
    print '*************************************************************************************'  
	print 'Finished setting database $(DatabaseName) attributes.' 
    print '*************************************************************************************'  
    print ''
end
